package com.jsl.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import com.jsl.NCCUtil;
import com.jsl.NCCloudService;
import com.jsl.ResultInfo;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.HashMap;
import java.util.Map;

public class AddProjectAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo info) {
        writeLog("**********************进入 AddProjectAction **********************");

        String requestid = info.getRequestid();
        String workflowid = info.getWorkflowid();
        RequestManager requestManager = info.getRequestManager();
        String formtable = NCCUtil.getFormtable(workflowid);

        writeLog("[AddProjectAction.execute] workflowid:" + workflowid + " formtable:" + formtable + " requestid:" + requestid);
//        //准备请求体数据
//        Map<String, Object> paramsMap = new HashMap<>();
//        //实际开始日期
//        paramsMap.put("plan_start_date","2024-08-10");
//        //实际完成日期
//        paramsMap.put("plan_finish_date","2024-08-20");
//        //期初
//        paramsMap.put("begin_flag","N");
//        //启用状态
//        paramsMap.put("enablestate",2);
//        //
//        paramsMap.put("project_code","Test_123");
//        paramsMap.put("project_name","接口测试");
//        paramsMap.put("pk_group","0001A110000000000DNJ");
//        paramsMap.put("pk_org","0001A110000000002PNU");
//        paramsMap.put("pk_org_v","0001A110000000002PNT");
//        String requestBody = JSON.toJSONString(paramsMap);

        String requestBody = "";
        String result = ""; // 返回结果
        String sql = "";
        boolean flag = true;
        RecordSet recordSet = new RecordSet();
        try {
            sql = "select * from " + formtable + " where requestid=" + requestid;
            recordSet.execute(sql);
            if (recordSet.next()) {
                Map<String, Object> paramsMap = new HashMap<>();
                String xmbh = recordSet.getString("xmbh"); // 项目编号
                String xmmc = recordSet.getString("xmmc"); // 项目名称
                String jhwcrq = recordSet.getString("jhwcrq"); // 计划完成日期
                String jhksrq = recordSet.getString("jhksrq"); // 计划开始日期
                requestBody = JSON.toJSONString(getRequestMap(xmbh,xmmc,jhksrq,jhwcrq));
                writeLog("[AddProjectAction.execute] requestBody:" + requestBody);
                String apiUrl = "/nccloud/api/uapbd/projectmanage/project/add";

                // 拿到返回数据，传入apiUrl、请求参数
                result = NCCloudService.pushData(apiUrl, requestBody);

                JSONValidator validator = JSONValidator.from(result);
                ResultInfo resultMap = null;
                String code = "";


                writeLog("[AddProjectAction.execute] resultMap:"+resultMap);

            }

        } catch (Exception e) {
            flag=false;

            writeLog(e);

        }
        return null;
    }



    private static Map<String ,Object> getRequestMap(String projectCode,String ProjectName,String startDate,String finishDate){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // Create a map representing the JSON object
            Map<String, Object> ufinterfaceMap = new HashMap<>();
            ufinterfaceMap.put("account", "ncc_hbjsl");
            ufinterfaceMap.put("billtype", "project");
            ufinterfaceMap.put("isexchange", "Y");
            ufinterfaceMap.put("replace", "Y");
            ufinterfaceMap.put("sender", "default");
            ufinterfaceMap.put("groupcode", "0002");

            Map<String, Object> billMap = new HashMap<>();
            billMap.put("billhead", createBillHeadMap(projectCode,ProjectName,startDate,finishDate));

            ufinterfaceMap.put("bill", billMap);
            resultMap.put("ufinterface",ufinterfaceMap);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    private static Map<String, Object> createBillHeadMap(String projectCode,String ProjectName,String startDate,String finishDate) {
        Map<String, Object> billHeadMap = new HashMap<>();
        billHeadMap.put("pk_group", "0001A110000000000DNJ");
        billHeadMap.put("pk_org", "0001A110000000002PNU");
        billHeadMap.put("pk_org_v", "0001A110000000002PNT");
        billHeadMap.put("project_code", projectCode);
        billHeadMap.put("project_name", ProjectName);
        billHeadMap.put("project_sh_name", "");
        billHeadMap.put("pk_eps", "1001A1100000000NABIM");
        billHeadMap.put("enablestate", "2");
        billHeadMap.put("check_date", "");
        billHeadMap.put("plan_start_date", startDate);
        billHeadMap.put("plan_finish_date", finishDate);
        billHeadMap.put("actu_start_date", "");
        billHeadMap.put("actu_finish_date", "");
        billHeadMap.put("memo", "a");
        billHeadMap.put("status_date", "");
        billHeadMap.put("planduration", "0");
        billHeadMap.put("actuduration", "0");
        billHeadMap.put("order_start_date", "");
        billHeadMap.put("order_finish_date", "");
        billHeadMap.put("bill_type", "4D10");
        billHeadMap.put("transi_type", "4D10-01");
        billHeadMap.put("req_start_date", "");
        billHeadMap.put("req_finish_date", "");
        billHeadMap.put("estimate_mny", "");
        billHeadMap.put("estimate_group", "");
        billHeadMap.put("estimate_global", "");
        billHeadMap.put("general_mny", "");
        billHeadMap.put("general_group", "");
        billHeadMap.put("general_global", "");
        billHeadMap.put("upload_flag", "N");
        billHeadMap.put("tax_flag", "N");

        Map<String, Object> bodyVosMap = new HashMap<>();
        bodyVosMap.put("item", createItemMap());

        billHeadMap.put("bodyvos", bodyVosMap);
        billHeadMap.put("begin_flag", "N");

        return billHeadMap;
    }

    /**
     * 默认组织为湖北金盛兰集团的金盛兰冶金科技有限公司
     * @return
     */
    private static Map<String, Object> createItemMap() {
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("pk_group", "0001A110000000000DNJ");
        itemMap.put("pk_org", "0001A110000000002PNU");
        itemMap.put("pk_org_v", "0001A110000000002PNT");
        itemMap.put("pk_parti_org", "0001A110000000002PNU");
        itemMap.put("pk_parti_org_v", "0001A110000000002PNT");
        itemMap.put("memo", "");
        return itemMap;
    }
}
